package com.fr.report.script;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.DeathCycleException;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ParameterMapNameSpace
  implements NameSpace
{
  protected Calculator nativeCalculator = Calculator.createCalculator();
  private Map nativeParameterMap = new HashMap();
  private static final Object CUR_VAR = new Object();

  public static ParameterMapNameSpace create(Parameter[] paramArrayOfParameter)
  {
    int i;
    HashMap localHashMap = new HashMap();
    if (paramArrayOfParameter != null)
      for (i = 0; i < paramArrayOfParameter.length; ++i)
        localHashMap.put(paramArrayOfParameter[i].getName(), paramArrayOfParameter[i].getValue());
    return create(localHashMap);
  }

  public static ParameterMapNameSpace create(Map paramMap)
  {
    return new ParameterMapNameSpace(paramMap);
  }

  protected ParameterMapNameSpace(Map paramMap)
  {
    if (paramMap != null)
    {
      Iterator localIterator = paramMap.entrySet().iterator();
      while (localIterator.hasNext())
      {
        Map.Entry localEntry = (Map.Entry)localIterator.next();
        key2UpperCase(localEntry.getKey(), localEntry.getValue());
      }
    }
    this.nativeCalculator.pushNameSpace(this);
    this.nativeCalculator.setAttribute(CUR_VAR, new HashSet());
  }

  public Function getMethod(Object paramObject, Calculator paramCalculator)
  {
    return null;
  }

  public Object getCellElementAttribute(ColumnRow paramColumnRow, Object paramObject, Calculator paramCalculator)
  {
    return null;
  }

  public Object getVariable(Object paramObject, Calculator paramCalculator)
  {
    if (paramObject == null)
      return null;
    Set localSet = (Set)this.nativeCalculator.getAttribute(CUR_VAR);
    if (localSet.contains(paramObject.toString()))
      throw new DeathCycleException("Death cycle exists at calculating " + ((String)paramObject));
    localSet.add(paramObject.toString());
    String str1 = paramObject.toString();
    if (str1.startsWith("$"))
      str1 = str1.substring(1);
    Object localObject = findKey(str1);
    if (localObject instanceof Formula)
    {
      String str2 = ((Formula)localObject).getContent();
      try
      {
        localObject = this.nativeCalculator.eval(str2.substring(1));
      }
      catch (UtilEvalError localUtilEvalError)
      {
        FRContext.getLogger().log(Level.WARNING, "Attention: Parameter formula getVariable", localUtilEvalError);
        localObject = str2;
      }
      key2UpperCase(str1, localObject);
    }
    localSet.remove(paramObject.toString());
    return localObject;
  }

  private void key2UpperCase(Object paramObject1, Object paramObject2)
  {
    if (paramObject1 == null)
      return;
    this.nativeParameterMap.put(paramObject1.toString().toUpperCase(), paramObject2);
  }

  private Object findKey(String paramString)
  {
    if (paramString == null)
      return null;
    return this.nativeParameterMap.get(paramString.toUpperCase());
  }
}